<template>
	<view class="u-view-form-select-picker-component">
		<view class="input-row" @tap.stop="onOpenPicker" :style="{backgroundColor:disabled?disabledColor:''}">
			<view class="mask" @tap.stop="" v-if="disabled"></view>
			<view class="input-view">
				<text v-if="!innerLabel" class="placeholder">{{placeholder}}</text>
				<text v-else>{{innerLabel}}</text>
			</view>
			<view class="close-view" @tap.stop="onClear" v-if="innerLabel && clearable">
				<view class="close-circle">
					<u-icon
					    name="close"
					    size="11"
					    color="#ffffff"
					    customStyle="line-height: 12px"
					></u-icon>
				</view>
			</view>
			<view class="right-icon-view"><u-icon name="arrow-right"></u-icon></view>
		</view>
		
		<easy-picker :visible.sync="pickerVisible" :list="list" :value-field="valueField" :label-field="labelField" @select="onPickerSelect" />
	</view>
</template>

<script>
import EasyPicker from './EasyPicker.vue'
export default {
	name:'uViewFormSelectPickerComponent',
	components:{EasyPicker},
	emits:['input','select','clear'],
	props:{
		placeholder:{
			type:String,
			default:'请选择...'
		},
		value:{
			type:[String,Boolean,Number,null],
			default:null
		},
		list:{
			type:Array,
			default:function(){
				return []
			}
		},
		valueField:{
			type:String,
			default:'value'
		},
		labelField:{
			type:String,
			default:'label'
		},
		disabled:{
			type:Boolean,
			default:false
		},
		clearable:{
			type:Boolean,
			default:true
		},
		disabledColor:{
			type:String,
			default:'#f5f7fa'
		}
	},
	data(){
		return {
			innerValue:'',
			innerLabel:'',
			pickerVisible:false,
			initFlag:false
		}
	},
	watch:{
		value(newVal,oldVal){
			if (newVal!==this.innerValue) {
				this.innerValue = newVal;
			}
		},
		innerValue(newVal,oldVal){
			if (newVal!==this.value) {
				this.$emit('input',newVal)
			}
			if (newVal!==oldVal) {
				this.setLabelValue()
			}
		},
		list(){
			this.setInitData()
		}
	},
	methods:{
		onOpenPicker(){
			if (this.list.length<=0) return false
			uni.hideKeyboard()
			this.pickerVisible = true
		},
		onPickerSelect(value,selection,index){
			this.innerValue = value;
			this.$emit('select',value,selection,index)
		},
		setLabelValue(){
			let noMatch = true;
			for (let i=0;i<this.list.length;i++) {
				if (this.isBaseItem(this.list[i])) {
					if (this.list[i]===this.innerValue) {
						this.innerLabel = this.list[i];
						noMatch = false;
						break;
					}
				} else {
					if (this.list[i][this.valueField]===this.innerValue) {
						this.innerLabel = this.list[i][this.labelField];
						noMatch = false;
						break;
					}
				}
			}
			if (noMatch) {
				this.innerLabel = this.innerValue
			}
		},
		isBaseItem(item){
			if (typeof(item)==='string'||typeof(item)==='number'||typeof(item)==='boolean'||item===undefined||item===null){
				return true
			} else {
				return false
			}
		},
		setInitData(){
			this.innerValue = this.value;
			if (this.list.length===1) {
				if (!this.initFlag) {
					this.initFlag = true
					let _tempVal = null;
					if (!this.isBaseItem(this.list[0])) {
						_tempVal = this.list[0][this.valueField];
					} else {
						_tempVal = this.list[0];
					}
					if (!this.innerValue) {
						this.innerValue = _tempVal;
					}
				}
			}
		},
		onClear(){
			this.innerValue = null;
			this.$emit('clear')
		}
	},
	created() {
		this.setInitData()
	}
}
</script>

<style scoped lang="scss">
.u-view-form-select-picker-component{
	width: 100%;
	.input-row{
		width: 100%;
		min-height: 48rpx;
		border-radius: 8rpx;
		overflow: hidden;
		background-color: $uni-bg-color;
		display: flex;
		align-items: center;
		position: relative;
		.close-view,.right-icon-view{
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.close-view{
			.close-circle{
				width: 20px;
				height: 20px;
				border-radius: 50%;
				background-color: #c6c7cb;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				transform: scale(0.82);
				margin-left: 4px;
			}
		}
		.input-view{
			flex-grow: 1;
			font-size: 30rpx;
			white-space:nowrap;
			overflow:hidden;
			text-overflow:ellipsis;
			color: rgb(48, 49, 51);
			.placeholder{
				color: rgb(192, 196, 204);
			}
		}
		&>.mask{
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 100;
		}
	}
}
</style>